using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms;
using Athena.Objects;


namespace Athena.Tools
{
     class GroupTool : INonVisualTool
    {


        public GroupTool()
        {
            _bHasContextMenuItems = true;
            _bRequiresNewToolWindow = true;
            _bHasMenuItems = false;

            ToolManager.Instance.RegisterTool("Group Tool", "Groups / Ungroups selected elements.", this);
        }

        public override void SetupToolStrip(ToolStrip oToolstrip)
        {
            {
                ToolStripButton oItem = new ToolStripButton("Group Elements");
                oItem.ToolTipText = "Group Elements";
                oItem.Image = global::Athena.Properties.Resources.ICON_Group;
                oItem.ImageScaling = ToolStripItemImageScaling.None;
                oItem.DisplayStyle = ToolStripItemDisplayStyle.Image;
                oItem.Click += new EventHandler(delegate { GroupSelected(); });
                
                oToolstrip.Items.Add(oItem);
            }
            {
                ToolStripButton oItem = new ToolStripButton("Ungroup Elements");
                oItem.ToolTipText = "Ungroup Elements";
                oItem.Image = global::Athena.Properties.Resources.ICON_Ungroup;
                oItem.ImageScaling = ToolStripItemImageScaling.None;
                oItem.DisplayStyle = ToolStripItemDisplayStyle.Image;
                oItem.Click += new EventHandler(delegate { UngroupSelected(); });
                
                oToolstrip.Items.Add(oItem);
            }
        }
        public override void SetupContextMenu(ToolStripMenuItem oStrip)
        {
            if (_bEnableGrouping)
            {
                ToolStripMenuItem oItem = new ToolStripMenuItem("Group Elements");
                oItem.ToolTipText = "Group Elements";
                oItem.Image = global::Athena.Properties.Resources.ICON_Group;
                oItem.ImageScaling = ToolStripItemImageScaling.None;
                oItem.Click += new EventHandler(delegate { GroupSelected(); });
                oStrip.DropDownItems.Add(oItem);
            }
            if (_bEnableUnGrouping)
            {
                ToolStripMenuItem oItem = new ToolStripMenuItem("Ungroup Elements");
                oItem.ToolTipText = "Ungroup Elements";
                oItem.Image = global::Athena.Properties.Resources.ICON_Ungroup;
                oItem.ImageScaling = ToolStripItemImageScaling.None;
                oItem.Click += new EventHandler(delegate { UngroupSelected(); });                
                oStrip.DropDownItems.Add(oItem);
            }
        }

        bool _bEnableGrouping = false;
        bool _bEnableUnGrouping = false;

        static Type IGlyphType = typeof(IGlyph);
        static Type GroupType = typeof(GroupGlyph);

        public override bool Handles(List<Type> oTypes)
        {
            _bEnableGrouping = true;
            _bEnableUnGrouping = false;
                        
            foreach (Type t in oTypes)
            {
                _bEnableGrouping &= Utility.IsType(IGlyphType, t) | oTypes.Count > 1;
                _bEnableUnGrouping |= Utility.IsType(GroupType, t);                
            }
            return _bEnableGrouping | _bEnableUnGrouping;
        }

        public void GroupSelected()
        {
            HistoryManager.History.SetControllPoint("Grouping Elements");
            
            IDrawable[] toDelete = CurrentModel.Selected.ToArray();
            CurrentModel.DeselectItems();

            foreach (IDrawable d in toDelete)
            {
                CurrentModel.Drawables.Remove(d);
            }

            CurrentModel.Drawables.Add(new GroupGlyph(toDelete));
            Utility.Refresh();

        }

        public void UngroupSelected()
        {

            HistoryManager.History.SetControllPoint("Ungrouping Elements");

            IDrawable[] toDelete = CurrentModel.Selected.ToArray();
            CurrentModel.DeselectItems();

            foreach (IDrawable glyph in toDelete)
            {
                if (!(glyph is GroupGlyph)) continue;
                GroupGlyph d = glyph as GroupGlyph;
                List<IDrawable> oItems = d.GroupedElements;
                foreach (IDrawable item in oItems)
                {
                    if (!(item is BezierConnector))
                    {
                        item.Location = Utility.AddPoints(item.Location, d.Location);
                        item.Dimensions = new System.Drawing.SizeF(
                            item.Dimensions.Width * d.Bounds.Width / d.OriginalBounds.Width,
                            item.Dimensions.Height * d.Bounds.Height / d.OriginalBounds.Height);
                    }
                    else
                    {
                        ((BezierConnector)item).MakePretty();
                    }
                    CurrentModel.Drawables.Add(item);
                }
                d.RestoreOldConnections();
                CurrentModel.DeselectItem(d);
                CurrentModel.Drawables.Remove(d);
            }

            Utility.Refresh();

        }

        public override void OnKeyDown(KeyEventArgs args)
        {
            switch (args.KeyCode)
            {
                case Keys.G:
                    GroupSelected();
                    break;
                case Keys.U:
                    UngroupSelected();
                    break;
                default:
                    break;
            }
        }


    }
}
